"""
    SuperPocketCube 扩展了更多功能
    打乱魔方、显示其它面等
"""

import PocketCube
import random


class SuperPocketCube(PocketCube.PocketCube):

    def shuffle(self):
        for _ in range(random.randint(10, 20)):
            configs = (random.randint(0, 5), random.choice([True, False]), random.randint(1, 2))
            # print(configs)
            self.rotate(*configs)

    def observeOneFaceByStrIndex(self, direction: str) -> list:
        return self.observeOneFace(list("FBUDLR").index(direction))

    def observeOneFace(self, direction: int) -> list:
        colorIndex = (int(direction / 2) + 1) % 3
        corners = self.getCorners(direction)

        blockFaces = [
            corner.color[colorIndex]
            for corner in corners
        ]

        return blockFaces

    # observeFacesInIntDirectionOrder
    def observeFaces(self) -> list:
        blockFaces = []
        for direction in range(6):
            blockFaces += self.observeOneFace(direction)

        return blockFaces

    def observeFacesInNativeOrder(self) -> list:
        blockFaces = []
        for corner in self.corners:
            for index in range(3):
                blockFaces.append(corner.color[index])
        return blockFaces

    @property
    def chaosLevel(self) -> float:
        score = 0
        for direction in range(6):
            colorsSet = set(self.observeOneFace(direction))
            score += len(colorsSet) - 1
        return score / 18

    def printState(self):
        print(self.show_front_up_right_view())
        print(self.observeFaces())
        print(f"chaos level: {self.chaosLevel}")

    def rotate_action(self, action_id: int):
        """
        公式  direction   clockwise   action_id
        F       0          true        0
        B       1          true        1
        U       2          true        2
        D       3          true        3
        L       4          true        4
        R       5          true        5
        F'      0          false       6
        B'      1          false       7
        U'      2          false       8
        D'      3          false       9
        L'      4          false       10
        R'      5          false       11
        """

        clockwiseInt, direction = divmod(action_id, 6)
        clockwiseBool = True if clockwiseInt == 0 else False

        self.rotate(direction, clockwiseBool)


if __name__ == '__main__':
    # 实例化你的二阶魔方
    pocketCube = SuperPocketCube()
    pocketCube.printState()

    # 执行一段魔方公式
    # pocketCube.act_formula("UR'2U'L2")  # "UR'2U'L2"

    # 查看魔方结果

    pocketCube.shuffle()

    pocketCube.printState()

